package br.com.timecardforpeon.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Util {

	public static final String DATE_FORMAT_FOR_VIEW = "MM/dd/yyyy";
	
	private static SimpleDateFormat formatter = new SimpleDateFormat();
	private static DecimalFormat decimalFormatter = new DecimalFormat("00");

	@SuppressWarnings("unchecked")
	public static <T> T getViewById(Activity activity, int id) {
		return (T) activity.findViewById(id);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getViewById(Dialog dialog, int id) {
		return (T) dialog.findViewById(id);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getViewById(View view, int id) {
		return (T) view.findViewById(id);
	}

	public static <T extends EditText> String getEditText(Activity activity, int id) {
		return Util.<T> getViewById(activity, id).getText().toString();
	}
	
	public static <T extends EditText> String getEditText(Dialog dialog, int id) {
		return Util.<T> getViewById(dialog, id).getText().toString();
	}

	public static <T extends TextView> void setViewText(Activity activity, int id, String text) {
		Util.<T> getViewById(activity, id).setText(text);
	}
	
	public static <T extends TextView> void setViewText(Dialog dialog, int id, String text) {
		Util.<T> getViewById(dialog, id).setText(text);
	}

	public static String dateToString(String pattern, Date date) {
		formatter.applyPattern(pattern);
		return formatter.format(date);
	}
	
	public static Date stringToDate(String pattern, String date) throws ParseException {
		formatter.applyPattern(pattern);
		return formatter.parse(date);
	}

	public static String dateToString(Date date) {
		return dateToString("yyyy-MM-dd", date);
	}
	
	public static Date stringToDate(String date) throws ParseException {
		return stringToDate("yyyy-MM-dd", date);
	}
	
	public static String dateTimeToString(Date date) {
		return dateToString("yyyy-MM-dd HH:mm:ss.SSS", date);
	}
	
	public static Date stringToDateTime(String date) throws ParseException {
		return stringToDate("yyyy-MM-dd HH:mm:ss.SSS", date);
	}

	public static Date dateWithoutHourMinuteSecondAndMillisecond(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		return calendar.getTime();
	}

	public static String getTime(Date date) {

		if (date == null) {
			return "";
		}

		formatter.applyPattern("HH:mm");
		return formatter.format(date);
	}
	
	public static String getTotalHours(int minutes) {

		return new StringBuilder()
				.append(decimalFormatter.format((int) (minutes / 60)))
				.append(":")
				.append(decimalFormatter.format((minutes % 60)))
				.toString();
	}
}